package cn.zh.npro.Activity;

import android.app.Dialog;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.npro.Item.CaItem;
import cn.zh.npro.Item.EcItem;
import cn.zh.npro.Item.ReItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;

public class EditClient extends BaseActivity {
   ImageView edback;
   CaItem caItem=null;

    EditText ecname,ecwork,edphone,ceadre;
    TextView compte;
    String linkmanid,customerid,postdate;
    String isdel,isMaster;
    String type;
    MyApplication mapp;
    private Dialog LDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_client);
        type=getIntent().getStringExtra("type");
        mapp= (MyApplication) getApplication();
        inited();
        if (type.equals("0")){
            caItem= (CaItem) getIntent().getSerializableExtra("ob");
            getpost();
         }

       }


    public void inited(){
        edback=findViewById(R.id.edback);
        ecname=findViewById(R.id.ecname);
        ecwork=findViewById(R.id.ecwork);
        edphone=findViewById(R.id.edphone);
        ceadre=findViewById(R.id.ceadre);
        compte=findViewById(R.id.compte);
        edback.setOnClickListener(this);

        compte.setOnClickListener(this);

        }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.edback:
                finish();
                break;
            case R.id.compte:
                if (type.equals("0")){
                    submit();
                }else {
                    submit1();
                 }
                break;
             }
           }




      public void getpost(){
          OkHttpUtils.get(BaseUrl.LinkmanDetail)
                  .params("linkmanid",caItem.getLinkmanid())
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          try {
                              JSONObject js=new JSONObject(s);
                              boolean State=js.getBoolean("State");
                              if (State){
                                 JSONObject Res=js.getJSONObject("Res");
                                 String fullname=Res.getString("fullname");
                                 String mobile=Res.getString("mobile");
                                 String jobtitle=Res.getString("jobtitle");
                                 String address=Res.getString("address");

                                  linkmanid = Res.getString("LinkmanID");
                                  customerid = Res.getString("customerid");
                                  isdel = Res.getString("isdel");
                                  isMaster = Res.getString("isMaster");
                                  postdate = Res.getString("postdate");
                                  ecname.setText(fullname);
                                  ecwork.setText(jobtitle);
                                  edphone.setText(mobile);
                                  ceadre.setText(address);

                               }

                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                      }
                  });
                }


         public void submit(){
             String nam=ecname.getText().toString();
             String job=ecwork.getText().toString();
             String phone=edphone.getText().toString();
             String adre=ceadre.getText().toString();
             if (nam.equals("")){
                 ToastUtil.showLong(EditClient.this,"请填写联系人姓名");
                 return;
              }
             if (phone.equals("")){
                 ToastUtil.showLong(EditClient.this,"请填写联系人手机号");
                 return;
              }
            LDialog= Loading.createLoadingDialog(this,"加载中...");
            EcItem ecItem=new EcItem(isdel,phone,postdate,adre,nam,linkmanid,isMaster,customerid,job);
            String str=new Gson().toJson(ecItem);
            OkHttpUtils.post(BaseUrl.LinkmanEdit)
                   .postJson(str)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                            if (null!=LDialog){
                                Loading.closeDialog(LDialog);
                            }
                           try {
                               JSONObject js=new JSONObject(s);
                               boolean State=js.getBoolean("State");
                               String Msg=js.getString("Msg");
                               if (State){
                                   ToastUtil.showLong(EditClient.this,Msg);
                                   finish();
                               }else {
                                   ToastUtil.showLong(EditClient.this,Msg);
                               }
                           } catch (JSONException e) {
                               e.printStackTrace();
                           }
                       }

                       @Override
                       public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                           super.onError(isFromCache, call, response, e);
                       }
                   });

          }









    public void submit1(){
        String nam=ecname.getText().toString();
        String job=ecwork.getText().toString();
        String phone=edphone.getText().toString();
        String adre=ceadre.getText().toString();
        if (nam.equals("")){
            ToastUtil.showLong(EditClient.this,"请填写联系人姓名");
            return;
          }
         if (phone.equals("")){
             ToastUtil.showLong(EditClient.this,"请填写联系人手机号");
             return;
         }

        LDialog= Loading.createLoadingDialog(this,"加载中...");
        ReItem reItem=new ReItem(false,phone,adre,nam,mapp.getClID(),false,job);
        String str=new Gson().toJson(reItem);
        OkHttpUtils.post(BaseUrl.LinkmanEdit)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------------>编辑联系人2232=="+"type=="+type+"=="+s);
                        if (null!=LDialog){
                            Loading.closeDialog(LDialog);
                          }
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            String Msg=js.getString("Msg");
                            if (State){
                                ToastUtil.showLong(EditClient.this,Msg);
                                finish();
                            }else {
                                ToastUtil.showLong(EditClient.this,Msg);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });

    }



    }
